package cn.edu.nuc.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


import cn.edu.nuc.ssm.entity.Gcar;
import cn.edu.nuc.ssm.entity.Goods;
import cn.edu.nuc.ssm.entity.GoodsList;
import cn.edu.nuc.ssm.entity.User;
import cn.edu.nuc.ssm.services.interfaces.GcarService;
import cn.edu.nuc.ssm.services.interfaces.GoodsService;
import cn.edu.nuc.ssm.services.interfaces.UserService;
import cn.edu.nuc.ssm.util.PageInfo;

@Controller
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private GcarService gcarService;
	
	
	@RequestMapping(value="/userlogin",method=RequestMethod.GET)
	public String login2(){
		
		return "user-sign-in";
	}
	
	@RequestMapping(value="/userlogin",method=RequestMethod.POST)
	public String login(User user,HttpSession session){
			
			
				try {
					user = userService.login(user.getUname(), user.getUpwd());
					
					session.setAttribute("user", user);
					return "redirect:/u/" + user.getUid() + "/home";
				} catch (Exception e) {
					e.printStackTrace();
				}		
					
			return "redirect:userlogin";
		}
	
	
	@RequestMapping(value="/resgister",method=RequestMethod.GET)
	public String resgister2(){
		
		return "UserRegister";
	}
	@RequestMapping(value="/resgister",method=RequestMethod.POST)
	public String resgister(User user,HttpSession session){
			
			    String smg ="用户名重复";
				try {					
				  int ref =	userService.register(user);
				  if (ref == 0) {
					throw new RuntimeException("用户名重复");
				}
				return "redirect:userlogin";
				} catch (Exception e) {
					e.printStackTrace();
				}		
				session.setAttribute("smg", smg);		
			return "redirect:resgister";
		}
	
	
	
	
	
	@RequestMapping(value="/u/{uid}/home",method=RequestMethod.GET)
	public String index(
			@PathVariable("uid") int uid,
			@RequestParam(name="current",defaultValue="1") int current,
			HttpSession session, 
			Model model){
		User user =(User) session.getAttribute("user");
		
		
		if( user == null || user.getUid() != uid ){
			return "redirect:/userlogin";
		}
		
		
		PageInfo pi = goodsService.namelist("",current);
		model.addAttribute("pi", pi);
		return "user-goods";
	}
	
	
	
	@RequestMapping(value="/u/{uid}/home",method=RequestMethod.POST)
	public String indexname(
			String gname,
			@RequestParam(name="current",defaultValue="1") int current,
			HttpSession session, 
			Model model){

		PageInfo pi = goodsService.namelist(gname,current);
		model.addAttribute("pi", pi);
		return "user-goods";
	}
	
	
	@RequestMapping(value="/u/{gid}/add",method=RequestMethod.GET)
	public String toedit(
			@PathVariable("gid") int gid,
			Model model,
			HttpSession session
			){	
		User user = (User) session.getAttribute("user");
		Goods goods = goodsService.findById(gid);
		model.addAttribute("goods", goods);
		model.addAttribute("user", user);
		return "UserGoodDetail";
	}
	
	
	@RequestMapping(value="/u/{gid}/add",method=RequestMethod.POST)
	public String edit(
			Gcar gc,
			HttpSession session
			){	
		User user = (User) session.getAttribute("user");
		gc.setUid(user.getUid());
		
		try {
			
			int ref = gcarService.add(gc);
			if(ref != 0){
				return "redirect:/u/" + user.getUid() + "/home";
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}	
		return "redirect:/u/" + gc.getGid() + "/add";
	}
	
	
	@RequestMapping(value="/u/{uid}/car",method=RequestMethod.GET)
	public String tocar(
			@PathVariable("uid") int uid,
			Model model,
			HttpSession session
			){	
		List<GoodsList> goodsLists = new ArrayList<>();		
		User user = (User) session.getAttribute("user");
		List<Gcar> gclist= gcarService.list(user.getUid());
		double price = 0;
		for (Gcar gcar : gclist) {
			GoodsList gcgood = new GoodsList();
			gcgood.setGoods(goodsService.findById(gcar.getGid()));
			gcgood.setNum(gcar.getCgoodsnum());
			price += (gcgood.getGoods().getGprice())*gcar.getCgoodsnum();
			goodsLists.add(gcgood);		
		}
		model.addAttribute("list", goodsLists);
		
		model.addAttribute("price",price);
		model.addAttribute("user",user);
		return "shopping-car";
	}
	@RequestMapping(value="/u/{gid}/car/delt",method=RequestMethod.GET)
	public String delt(
			@PathVariable("gid") int gid,
			Model model,
			HttpSession session
			){
		
		User user =(User) session.getAttribute("user");
		int ref= gcarService.delt(user.getUid(), gid);
		if (ref==0) {
			return "redirect:/u/"+user.getUid()+"/car";
		}
		return "redirect:/u/"+user.getUid()+"/car";
		}
	
	@RequestMapping(value="/u/{uid}/car/pay",method=RequestMethod.GET)
	public String pay(
			@PathVariable("uid") int uid,
			Model model,
			HttpSession session
			){
		
		User user =(User) session.getAttribute("user");
		
		 try {
			gcarService.pay(uid);
		} catch (Exception e) {			
			e.printStackTrace();
		}
		 return "redirect:/u/" + user.getUid() + "/home";
	}
	
}
